#!/bin/bash
# LNMP For Ubuntu
# Date: 2013-12-31
CWD=$(pwd)
. ./functions/soft_version.sh
. ./functions/mysql.sh
. ./functions/system.sh
. ./functions/logs.sh
. ./functions/nginx.sh
. ./functions/php4nginx.sh
. ./functions/php_modules.sh

logs "Info" "System Check"
system_check
welcome
check_current_user

echo -ne "Press any key to start install \033[32;49;1mLNMP\033[39;49;0m ... "
char=`get_char`
echo
echo

logs "Info" "Begin Install"

# 时间校对
#NTP_CN

#系统优化
#Optimization

############# Install Dep Packages ##############
logs "Info" "Install System packages"

ubuntu_packages() {
apt-get update
apt-get -y remove apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker mysql-client mysql-server mysql-common php5 php5-common php5-cgi php5-mysql php5-curl php5-gd
killall apache2
apt-get purge `dpkg -l | grep php| awk '{print $2}'`
#apt-get update
apt-get -y autoremove
#apt-get -fy install
apt-get -y install build-essential gcc g++ make cmake

for pkg in build-essential gcc g++ make cmake automake autoconf re2c wget cron bzip2 libzip-dev libc6-dev file rcconf flex vim nano bison m4 gawk less make cpp binutils diffutils unzip tar bzip2 libbz2-dev unrar p7zip libncurses5-dev libncurses5 libtool libevent-dev libpcre3 libpcre3-dev libpcrecpp0 libgd2-xpm-dev libssl-dev zlibc openssl libsasl2-dev libltdl3-dev libltdl-dev mcrypt libmcrypt-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libfreetype6 libfreetype6-dev libjpeg62 libjpeg62-dev libjpeg-dev libpng-dev libpng12-0 libpng12-dev curl libcurl3 libmhash2 libmhash-dev libpq-dev libpq5 gettext libncurses5-dev libcurl4-gnutls-dev libjpeg-dev libpng12-dev libxml2-dev zlib1g-dev libfreetype6 libfreetype6-dev libssl-dev libcurl3 libcurl4-openssl-dev libcurl4-gnutls-dev libcap-dev patch
	do
		apt-get -y install $pkg
		#apt-get -fy install
		#apt-get -y autoremove
	done
}

ubuntu_packages

############### Install MySQL #################
logs "Info" "Install MySQL"
# 调用安装函数
MySQL_Install

############ Ready for install PHP  ##############
logs "Info" "Install libiconv"

cd $CWD
UbR=`cat /etc/issue | head -1 | awk '{print $2}' | cut -d "." -f 1`
if [ $UbR -lt 13 ] ; then
  tar -zxf libiconv-1.14.tar.gz
  cd libiconv-1.14
  ./configure --prefix=/usr/local
  make && make install
  cd ..
else
  wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
  tar -zxf libiconv-1.13.1.tar.gz
  cd libiconv-1.13.1
  ./configure --prefix=/usr/local
  make && make install
  cd ..
fi

#tar -zxf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8
#./configure
#make && make install
#/sbin/ldconfig
#cd libltdl/
#./configure --enable-ltdl-install
#make && make install
#cd ..
apt-get install libmcrypt-dev -y

#cd $CWD
#tar -zxf mhash-0.9.9.9.tar.gz
#cd mhash-0.9.9.9
#./configure
#make && make install
#cd ..

apt-get install libmhash2 -y
apt-get install libmhash-dev -y

#if [ "$bit" = "x64" ]; then
#ln -s /usr/local/lib/libmcrypt.la /usr/lib/x86_64-linux-gnu/libmcrypt.la
#ln -s /usr/local/lib/libmcrypt.so /usr/lib/x86_64-linux-gnu/libmcrypt.so
#ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/x86_64-linux-gnu/libmcrypt.so.4
#ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/x86_64-linux-gnu/libmcrypt.so.4.4.8
#ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
#ln -s /usr/local/lib/libiconv.so.2 /usr/lib/x86_64-linux-gnu/libiconv.so.2
#ln -s /usr/local/lib/libmhash.a /usr/lib/x86_64-linux-gnu/libmhash.a
#ln -s /usr/local/lib/libmhash.la /usr/lib/x86_64-linux-gnu/libmhash.la
#ln -s /usr/local/lib/libmhash.so /usr/lib/x86_64-linux-gnu/libmhash.so
#ln -s /usr/local/lib/libmhash.so.2 /usr/lib/x86_64-linux-gnu/libmhash.so.2
#ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/x86_64-linux-gnu/libmhash.so.2.0.1
#else
#ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
#ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
#ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
#ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
#ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
#ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
#ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
#ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
#ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
#ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
#ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
#fi
 
cd $CWD

#tar -zxf mcrypt-2.6.8.tar.gz
#cd mcrypt-2.6.8
#./configure
#make && make install
#cd ..
apt-get install -y mcrypt

#if [ "$bit" = "x64" ]; then
#ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so 
#ln -s /usr/lib64/libldap.so /usr/lib/libldap.so 
#ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
#else
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
#fi

/sbin/ldconfig

############ Install PHP #############

[ "$phpv" = "5.3" ] && php53
[ "$phpv" = "5.2" ] && php52

############ PHP Modules Install #############
php_modules_install

############ Starting PHP #############
if [ "$phpv" = "5.3" ] ; then
   /etc/init.d/php-fpm start
else
   /usr/local/php/sbin/php-fpm start
fi

###############  Install Nginx ################
logs "Info" "Install Nginx $nginxVer"

cd $CWD
Nginx_Install

logs "Info" "Install phpMyAdmin $pmVer"
cd $CWD
tar -jxf phpMyAdmin-$pmVer.tar.bz2
mv phpMyAdmin-$pmVer /var/www/localhost/phpmyadmin
chown www.www -R /var/www/localhost

logs "Info" "Install Complete"
# get localhost IP address
ip=`ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | awk '{print $1}' | head -n 1`
echo
echo "Congratulations!! "
echo "Linux + Nginx + PHP is Sucessfully! "
echo
echo "========================== Installation information ====================="
echo "                 Install LNMP 2013(Final) completed! Enjoy it."
echo "========================================================================="
echo "        LNMP V2013 for Ubuntu .Written by Luxiaok.com "
echo "========================================================================="
echo ""
echo "     For more information please visit http://cto.luxiaok.com/"
echo ""
echo "index page: http://$ip/index.php"
echo "   phpinfo: http://$ip/info.php"
echo "phpMyAdmin: http://$ip/phpmyadmin/"
echo "   iProber: http://$ip/tz.php"
echo ""
#echo "default MySQL root password:$mysqlrootpwd"
echo ""
echo "The path of LNMP directorys:"
echo " MySQL DIR:   /usr/local/mysql"
echo "   PHP DIR:   /usr/local/php"
echo " Nginx DIR:   /usr/local/nginx"
echo "   WEB DIR:   /var/www"
echo " Vhost DIR:   /var/www/*/.vhost"
echo ""
echo "=========================================================================="

cat >> $CWD/tips.txt << EOF
Some information for LNMP:
     Index: http://$ip/index.php
   phpinfo: http://$ip/info.php
phpMyAdmin: http://$ip/phpmyadmin
   iProber: http://$ip/tz.php
    
Default MySQL root password is null

The path of LNMP directorys:
MySQL DIR: /usr/local/mysql
Conf file: /usr/lcao/mysql/etc/my.cnf
PHP  DIR : /usr/local/php
Conf file: /usr/local/php/etc/php.ini
Nginx DIR: /usr/local/nginx
Conf file: /usr/local/nginx/conf/nginx.conf
  WEB DIR: /var/www/
Vhost DIR: /var/www/*/.vhost
EOF
netstat -ntlup